package coml.jiyun.hp.gjtk_e.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import coml.jiyun.hp.gjtk_e.Main2Activity;
import coml.jiyun.hp.gjtk_e.MyEventBus;
import coml.jiyun.hp.gjtk_e.R;
import coml.jiyun.hp.gjtk_e.adapter.ListAdapter;
import coml.jiyun.hp.gjtk_e.bean.ListBean;
import coml.jiyun.hp.gjtk_e.presenter.NetPersencer;
import coml.jiyun.hp.gjtk_e.view.NetView;

public class HomeFragment extends Fragment implements NetView {
    private RecyclerView mRecy;
    private int id;
    private SmartRefreshLayout mSmall;
    int count = 1;
    private ArrayList<ListBean.DataBean.DatasBean> mData;
    private ListAdapter adapter;
    private NetPersencer persencer;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        initView(view);
        persencer = new NetPersencer(this);
        id = getArguments().getInt("id");
        persencer.getData(count, id);
        return view;
    }

    private void initView(@NonNull final View itemView) {

        mRecy = (RecyclerView) itemView.findViewById(R.id.recy);
        mSmall = (SmartRefreshLayout) itemView.findViewById(R.id.small);
        mRecy.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecy.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayout.VERTICAL));
        mData = new ArrayList<>();
        adapter = new ListAdapter(getActivity(), mData);
        mRecy.setAdapter(adapter);


        mSmall.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                count++;
                persencer.getData(count,id);
            }
        });

        mSmall.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                count=1;
                persencer.getData(count,id);
            }
        });

//        adapter.setOnItemClickListener(new ListAdapter.onItemClickListener() {
//            @Override
//            public void onItemClick(String link) {
//                EventBus.getDefault().postSticky(new MyEventBus(link));
//                Intent intent = new Intent(getActivity(), Main2Activity.class);
//                startActivity(intent);
//            }
//        });

//         adapter.setItemOnick(new ListAdapter.itemOnick() {
//             @Override
//             public void dian(String link) {
//                 EventBus eventBus = new EventBus();
//                 mData.get()
//                 eventBus.getDefault().postSticky(new MyEventBus(link));
//                 Intent intent = new Intent(getActivity(), Main2Activity.class);
//                 startActivity(intent);
//             }
//         });
        adapter.setItemOnick(new ListAdapter.itemOnick() {
            @Override
            public void dian(int position) {
                String link = mData.get(position).getLink();
                MyEventBus myEventBus = new MyEventBus();
                myEventBus.link = link;
                EventBus.getDefault().postSticky(myEventBus);
                startActivity(new Intent(getActivity(),Main2Activity.class));
            }
        });
    }

    @Override
    public void setToast(String str) {
        Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setData(ArrayList<ListBean.DataBean.DatasBean> data) {
        mData.addAll(data);
        adapter.notifyDataSetChanged();
    }
}
